<?php
/**
 * 系统食材表（食材升级必需品）
 *
 * @author river_he@eueang.com
 */
final class Kit_Model_Material extends Kit_Model_Abstract{
	/**
	 * 获取食材列表
	 * 
	 * @param string|where $where
	 * @param string $sort
	 * @param int $page
	 * @param int $psize
	 * @return array
	 */
	public function getItems($where, $sort='', $page=1, $psize=10){
		$page = $page>0?$page:1;
		$psize = $psize>0?$psize:10;
		$data = $this->getMaterialDao()->getItems($where, $sort, $page, $psize);
		$pager = array();
		if($data){
			$total = $this->getMaterialDao()->getWhereTotal($where);
			$pager = array(
			'page'=>$page,
			'psize'=>$psize,
			'ptotal'=>ceil($total/$psize),
			'rtotal'=>$total
			);
		}
		$return = array(
		'data' =>$data,
		'pager' =>$pager
		);
		return $return;
	}
	
	/**
	 * 销售食材
	 * 
	 * @param int $shoperId 店主id
	 * @param int $shoperMaterId 店主食材id 
	 * @param int $total 销售数量
	 * @return int 1:OK -1:no property -2:shoper wrong -3:prope already used -4:no shoper 
	 */
	public function sell($shoperId, $shoperMaterId, $total){
		$shopMater = $this->getShoperMaterialDao()->getItem('id='.$shoperMaterId);
		if(!$shopMater){
			return -1; 
		}elseif($shopMater['nUid'] != $shoperId){
			return -2;
		}elseif($total != $shopMater['nTotal']){
			return -3;
		}
		$shoper = $this->getShoperDao()->getItem('id='.$shopMater['nUid']);
		if(!$shoper){
			return -4;
		}
		$set = array();
		$mater = $this->getMaterialDao()->getItem('id='.$shopMater['nMid']);
		$pay = $total*$mater['nPrice'];
		//@todo 卖出对折？
		if(0 < $pay){
			if(1 == $shopProp['nPriceType']){
				$pay += $shoper['nGold'];
				$set['nGold'] = $pay;
			}elseif(2 == $shopProp['nPriceType']){
				$pay += $shopProp['nKb'];
				$set['nKb'] = $pay;
			}
		}
		if($set){
			if(!$this->getShoperDao()->update('id='.$shoper['id'], $set)){
				return -5;
			}
		}
		if($this->getShoperPropDao()->update('id='.$shopProp['id'], array('nStatus'=>3))){
			return 1;
		}
		return -6;
	}
	
	/**
	 * 购买食材
	 * 
	 * @param int $shoperId
	 * @param int $shopId
	 * @param int $mid
	 * @return int|array -1.no shoper -2.no shop -3.shop not belong shoper -4.no material
	 */
	public function buy($shoperId, $mid, $total){
		$mater = $this->getMaterialDao()->getItem('id='.$mid);
		if(!$mater){
			return -1;
		}
		$shoper = $this->getShoperDao()->getItem('id='.$shoperId);
		if(!$shoper){
			return -2; 
		}
		$pay = 1>$mater['nRebate']?ceil($mater['nRebate']*$mater['nPrice']):$mater['nPrice'];
		if(0 > $pay){
			return -3;
		}elseif(0 < $pay){
			$set = array();
			if(1 == $mater['nPriceType']){
				if($shoper['nGold'] < $pay){
					return -4;
				}else{
					$set['nGold'] = $shoper['nGold']-$pay;
				}
			}else if(2 == $mater['nPriceType']){
				if($shoper['nKb'] < $pay){
					return -5;
				}else{
					$set['nKb'] = $shoper['nKb']-$pay;
				}
			}
			//@todo 事务处理
			if($set){
				if(!$this->getShoperDao()->update('id='.$shoper['id'], $set)){
					return -6;
				}
			}
		}
		$hasWhere = array('nUid='.$shoper['id']);
		array_push($hasWhere, 'nMid='.$mater['id']);
		$hasMater = $this->getShoperMaterialDao()->getItem($hasWhere);
		if($hasMater){
			$hasMater['nTotal'] += $total;
			$this->getShoperMaterialDao()->update('id='.$hasMater['id'], array('nTotal'=>$hasMater['nTotal']));
			$this->getMaterialDao()->update('id='.$mater['id'], array('nSells'=>$mater['nSells']+$total));
			return $hasMater;
		}else{
			$record = array(
			'nUid'=>$shoper['id'],
			'nMid'=>$mater['id'],
			'sName'=>$mater['sName'],
			'nTotal'=>$total,
			'nCrTime'=>Kit_Util::getTimeStamp()
			);
			if($id = $this->getShoperMaterialDao()->add($record)){
				$this->getMaterialDao()->update('id='.$mater['id'], array('nSells'=>$hasMater['nSells']+$total));
				$record['id'] = $id;
				return $record;
			}
		}
		return -7;
	}
}
//end